package com.yy.bolg.controller.accessory;

import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.AccessoryBean;
import com.yy.bolg.entity.AccessoryClassBean;
import com.yy.bolg.request.AccessoryClassRequest;
import com.yy.bolg.request.AccessoryRequest;
import com.yy.bolg.request.CustomerRequest;
import com.yy.bolg.resp.AccessoryResp;
import com.yy.bolg.resp.AccessoryToClassResp;
import com.yy.bolg.service.AccessoryClassService;
import com.yy.bolg.service.AccessoryService;
import com.yy.bolg.util.CodeGenerateUtil;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 附件相关控制器
 * @author enmaoFu
 * @date 2024-11-04
 * RequestMapping("/accessory") 是说明这个类的访问地址是 /accessory
 */
@Api(tags = {"附件及附件分类"})
@Controller
@RequestMapping(value = "/accessory")
public class AccessoryController {

    /**
     * 自动载入Service对象
     */
    @Autowired
    private AccessoryService aService;
    @Autowired
    private AccessoryClassService acService;

    /**
     * 添加附件及附件分类
     * 服务器访问路径：http://localhost:8080/bolg/accessory/add
     * 本地访问路径：http://localhost:8111/accessory/add
     * 参数：
     * {"accessoryClassName": "开关","accessoryRequestList": [{"accessoryName": "公牛开关", "accessoryPrice": 38.54},{"accessoryName": "施耐德开关","accessoryPrice": 58.54},{"accessoryName": "飞利浦开关","accessoryPrice": 48.54}]}
     */
    @ApiOperation(value = "添加附件及附件分类", notes = "参数：{\"accessoryClassName\": \"开关\",\"accessoryRequestList\": [{\"accessoryName\": \"公牛开关\", \"accessoryPrice\": 38.54},{\"accessoryName\": \"施耐德开关\",\"accessoryPrice\": 58.54},{\"accessoryName\": \"飞利浦开关\",\"accessoryPrice\": 48.54}]}")
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String add(@RequestBody AccessoryClassRequest request){
        String result;

        try {
            String accessoryClassCode = CodeGenerateUtil.getAccessoryClassCode();
            AccessoryClassBean accessoryClassBean = new AccessoryClassBean();
            accessoryClassBean.setPsAccessoryClassCode(accessoryClassCode);
            accessoryClassBean.setPsAccessoryClassName(request.getAccessoryClassName());
            acService.add(accessoryClassBean);

            List<AccessoryRequest> accessoryBeans = request.getAccessoryRequestList();
            for(AccessoryRequest ar: accessoryBeans){
                AccessoryBean accessoryBean = new AccessoryBean();
                accessoryBean.setPsAccessoryCode(CodeGenerateUtil.getAccessoryCode());
                accessoryBean.setPsAccessoryName(ar.getAccessoryName());
                accessoryBean.setPsAccessoryPrice(ar.getAccessoryPrice());
                accessoryBean.setPsAccessoryClassCode(accessoryClassCode);
                accessoryBean.setPsAccessoryClassName(request.getAccessoryClassName());
                aService.add(accessoryBean);
            }

            Map map = new HashMap();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "添加成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 删除附件分类的同时，根据附件分类编号删掉对应的所有附件
     * 服务器访问路径：http://localhost:8080/bolg/accessory/delete/relation
     * 本地访问路径：http://localhost:8111/accessory/delete/relation
     * 参数：
     * {"accessoryClassCode":"ACCC20241104204909"}
     */
    @ApiOperation(value = "删除附件分类的同时，根据附件分类编号删掉对应的所有附件", notes = "参数：{\"accessoryClassCode\":\"ACCC20241104204909\"}")
    @RequestMapping(value = "/delete/relation",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String deleteRelation(@RequestBody AccessoryClassRequest request){
        String result;

        AccessoryClassBean accessoryClassBean = new AccessoryClassBean();
        accessoryClassBean.setPsAccessoryClassCode(request.getAccessoryClassCode());

        AccessoryBean accessoryBean = new AccessoryBean();
        accessoryBean.setPsAccessoryClassCode(request.getAccessoryClassCode());

        try {
            acService.del(accessoryClassBean);
            aService.delAccessoryClass(accessoryBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "删除成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据附件编号删掉对应的附件
     * 服务器访问路径：http://localhost:8080/bolg/accessory/delete
     * 本地访问路径：http://localhost:8111/accessory/delete
     * 参数：
     * {"accessoryCode":"ACC20241104345587"}
     */
    @ApiOperation(value = "根据附件编号删掉对应的附件", notes = "参数：{\"accessoryCode\":\"ACC20241104345587\"}")
    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String delete(@RequestBody AccessoryRequest request){
        String result;

        AccessoryBean accessoryBean = new AccessoryBean();
        accessoryBean.setPsAccessoryCode(request.getAccessoryCode());

        try {
            aService.delAccessory(accessoryBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "删除成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 查询所有附件分类及对应的所有附件
     * 服务器访问路径：http://localhost:8080/bolg/accessory/query/relation
     * 本地访问路径：http://localhost:8111/accessory/query/relation
     * 参数：
     */
    @ApiOperation(value = "查询所有附件分类及对应的所有附件", notes = "", response = AccessoryToClassResp.class)
    @RequestMapping(value = "/query/relation",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String queryRelation(){
        String result;

        try {
            List<AccessoryToClassResp> resp = new ArrayList<>();

            List<AccessoryClassBean> accessoryClassBeans = acService.findAll();

            for(AccessoryClassBean acb: accessoryClassBeans){
                AccessoryToClassResp atcr = new AccessoryToClassResp();
                atcr.setAccessoryClassCode(acb.getPsAccessoryClassCode());
                atcr.setAccessoryClassName(acb.getPsAccessoryClassName());

                AccessoryBean accessoryBean = new AccessoryBean();
                accessoryBean.setPsAccessoryClassCode(acb.getPsAccessoryClassCode());
                List<AccessoryBean> accessoryBeans = aService.findOneByAccessoryClassCode(accessoryBean);

                List<AccessoryResp> accessoryRespList = new ArrayList<>();

                for(AccessoryBean ab: accessoryBeans){
                    AccessoryResp ar = new AccessoryResp();
                    ar.setAccessoryCode(ab.getPsAccessoryCode());
                    ar.setAccessoryName(ab.getPsAccessoryName());
                    ar.setAccessoryPrice(ab.getPsAccessoryPrice());
                    accessoryRespList.add(ar);
                }

                atcr.setAccessoryRespList(accessoryRespList);

                resp.add(atcr);
            }

            Map map = new HashMap();
            map.put("data", resp);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 单独查询所有附件分类
     * 服务器访问路径：http://localhost:8080/bolg/accessory/query/calss
     * 本地访问路径：http://localhost:8111/accessory/query/calss
     * 参数：
     */
    @ApiOperation(value = "单独查询所有附件分类", notes = "", response = AccessoryToClassResp.class)
    @RequestMapping(value = "/query/calss",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String queryCalss(){
        String result;

        try {
            List<AccessoryToClassResp> resp = new ArrayList<>();
            List<AccessoryClassBean> accessoryClassBeans = acService.findAll();
            for(AccessoryClassBean acb: accessoryClassBeans){
                AccessoryToClassResp atcr = new AccessoryToClassResp();
                atcr.setAccessoryClassCode(acb.getPsAccessoryClassCode());
                atcr.setAccessoryClassName(acb.getPsAccessoryClassName());
                resp.add(atcr);
            }

            Map map = new HashMap();
            map.put("data", resp);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据附件分类编号查询对应的所有附件
     * 服务器访问路径：http://localhost:8080/bolg/accessory/query
     * 本地访问路径：http://localhost:8111/accessory/query
     * 参数：
     * {"accessoryClassCode":"ACCC20241104212801"}
     */
    @ApiOperation(value = "根据附件分类编号查询对应的所有附件", notes = "参数：{\"accessoryClassCode\":\"ACCC20241104212801\"}", response = AccessoryRequest.class)
    @RequestMapping(value = "/query",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String query(@RequestBody AccessoryRequest request){
        String result;

        try {
            AccessoryClassBean accessoryClassBean = new AccessoryClassBean();
            accessoryClassBean.setPsAccessoryClassCode(request.getAccessoryClassCode());

            AccessoryClassBean accessoryClassBeans = acService.findOneByCode(accessoryClassBean);

            List<AccessoryResp> resp = new ArrayList<>();
            AccessoryBean accessoryBean = new AccessoryBean();
            accessoryBean.setPsAccessoryClassCode(accessoryClassBeans.getPsAccessoryClassCode());
            List<AccessoryBean> accessoryBeans = aService.findOneByAccessoryClassCode(accessoryBean);
            for(AccessoryBean ab: accessoryBeans){
                AccessoryResp ar = new AccessoryResp();
                ar.setAccessoryCode(ab.getPsAccessoryCode());
                ar.setAccessoryName(ab.getPsAccessoryName());
                ar.setAccessoryPrice(ab.getPsAccessoryPrice());
                resp.add(ar);
            }

            Map map = new HashMap();
            map.put("data", resp);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

}
